package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;

import java.awt.event.*;
import java.util.Vector;
public class NPCXinxi extends JPanel implements MouseListener{
    DuobaoSwingLaunch game;
    Map heroMap;
    Vector vector;//用于分页
    int newPage;//当前页数
    NPC npc;//NPC
    JLabel jlb_nextPage;//下一页
    JLabel jlb_previousPage;//上一页
    JLabel fanhui;//返回
    JLabel fanhuiyouxi;//返回游戏




    JLabel chenghu;//名号
    JLabel suishu;//岁数
    JLabel chushou;//出手
    JLabel zhuangtai;//状态
    JLabel daiyou;//带有
    JLabel zhuangbei[];//装备
    JLabel baishi;//拜师
    JLabel panshi;//叛师
    JLabel xueyi;//学艺
    JLabel duihua;//对话
    JLabel qiecuo;//切磋
    JLabel shalu;//杀戮
    public NPCXinxi(DuobaoSwingLaunch game, NPC npc, int page)
    {
        vector=new Vector();
        this.newPage=page;
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        this.npc=npc;
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);

        setChenghu();
        setSuishu();
        setChushou();
        setZhuangtai();
        setZhuangbei();
        setBaishi_xueyi_pangshi();
        setDuihua();
        setQiecuo();
        setShalu();
        setFanhui();
        setFanhuiyouxi();
        setadd();
    }

    public void setChenghu() {
        chenghu=new JLabel(this.npc.getName());
        chenghu.addMouseListener(this);
        chenghu.setFont(myFont.f2);
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(chenghu);
        this.vector.add(jp3);
    }

    public void setSuishu() {
        suishu=new JLabel(this.npc.getsexMiShu()+"看起来大约"+this.npc.getAgeDa()+"多岁。");
        suishu.addMouseListener(this);
        suishu.setFont(myFont.f2);
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(suishu);
        this.vector.add(jp3);
    }

    public void setChushou() {
        chushou=new JLabel(this.npc.getsexMiShu()+"的武功看起来好像"+this.npc.getWGMS()+",出手似乎"+this.npc.getCSMS()+"。");
        chushou.addMouseListener(this);
        chushou.setFont(myFont.f2);
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(chushou);
        this.vector.add(jp3);
    }

    public void setZhuangtai() {
        zhuangtai=new JLabel(this.npc.getsexMiShu()+"看起来"+this.npc.getQxue()+"。");
        zhuangtai.addMouseListener(this);
        zhuangtai.setFont(myFont.f2);
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(zhuangtai);
        this.vector.add(jp3);
    }

    public void setZhuangbei() {


        int a = this.npc.getEquip().size();
        if(a!=0)
        {
            daiyou=new JLabel(this.npc.getsexMiShu()+"的身上带有：");
            daiyou.addMouseListener(this);
            daiyou.setFont(myFont.f2);
            JPanel jp3=new JPanel();
            jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp3.add(daiyou);
            this.vector.add(jp3);

        }
        zhuangbei = new JLabel[a];
        for(int i=0;i<a;i++)
        {
            Equip e = this.npc.getEquip().get(i);
            zhuangbei[i] = new JLabel("□"+e.getName());
            zhuangbei[i].addMouseListener(this);
            zhuangbei[i].setFont(myFont.f2);
            JPanel jp3=new JPanel();
            jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp3.add(zhuangbei[i]);
            this.vector.add(jp3);
        }
    }

    public void setBaishi_xueyi_pangshi()
    {
        int npcjiaopai=this.npc.getJiaoPai();
        int npcdaishu=this.npc.Daishu;
        int herojiaopai=this.game.hero.getJiaoPai();
        int herodaishu=this.game.hero.Daishu;
        if(npcjiaopai!=0)//不是普通百姓
        {
            if(herojiaopai==0 && this.npc.getBaishi())//自己是普通百姓
            {
                setbaishi();
            }else if(npcjiaopai==herojiaopai)//相同教派
            {
                if(this.npc==this.game.hero.getShifu())
                {
                    setXueyi();
                    setPanshi();
                }else if(npcdaishu==herodaishu+1)
                {
                    setbaishi();
                }
            }
        }
    }
    public void setbaishi()
    {
        baishi=new JLabel();
        baishi.addMouseListener(this);
        baishi.setFont(myFont.f2);
        baishi.setForeground(Color.BLUE);
        baishi.setText("<html><u>"+"拜师"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(baishi);
        this.vector.add(jp3);
    }
    public void setPanshi()
    {
        panshi=new JLabel();
        panshi.addMouseListener(this);
        panshi.setFont(myFont.f2);
        panshi.setForeground(Color.BLUE);
        panshi.setText("<html><u>"+"叛师"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(panshi);
        this.vector.add(jp3);
    }
    public void setXueyi()
    {
        xueyi=new JLabel();
        xueyi.addMouseListener(this);
        xueyi.setFont(myFont.f2);
        xueyi.setForeground(Color.BLUE);
        xueyi.setText("<html><u>"+"学艺"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(xueyi);
        this.vector.add(jp3);
    }
    public void setDuihua()
    {
        duihua=new JLabel();
        duihua.addMouseListener(this);
        duihua.setFont(myFont.f2);
        duihua.setForeground(Color.BLUE);
        duihua.setText("<html><u>"+"对话"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(duihua);
        this.vector.add(jp3);
    }
    public void setQiecuo()
    {
        qiecuo=new JLabel();
        qiecuo.addMouseListener(this);
        qiecuo.setFont(myFont.f2);
        qiecuo.setForeground(Color.BLUE);
        qiecuo.setText("<html><u>"+"切磋"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(qiecuo);
        this.vector.add(jp3);
    }
    public void setShalu()
    {
        shalu=new JLabel();
        shalu.addMouseListener(this);
        shalu.setFont(myFont.f2);
        shalu.setForeground(Color.BLUE);
        shalu.setText("<html><u>"+"杀戮"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(shalu);
        this.vector.add(jp3);
    }
    public void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.vector.add(jp3);
    }

    public void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.vector.add(jp4);
    }
    private void setadd()//加载JLabel
    {
        JPanel jPanel=new JPanel();
        jPanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        if(this.vector.size()<=this.newPage*15)
        {

            if(this.newPage!=1)
            {
                jlb_previousPage=new JLabel();
                jlb_previousPage.addMouseListener(this);
                jlb_previousPage.setFont(myFont.f2);
                jlb_previousPage.setForeground(Color.BLUE);
                jlb_previousPage.setText("<html><u>"+"上一页"+"</u></html>");
                jPanel.add(jlb_previousPage);
                this.vector.add(this.vector.size()-2,jPanel);
            }

            for(int i=((this.newPage-1)*15);i<this.vector.size();i++)
            {
                this.add((Component) this.vector.get(i));
            }
        }else
        {
            for(int i=((this.newPage-1)*15);i<(this.newPage)*15;i++)
            {
                this.add((Component) this.vector.get(i));
            }

            if(this.newPage!=1)
            {
                jlb_previousPage=new JLabel();
                jlb_previousPage.addMouseListener(this);
                jlb_previousPage.setFont(myFont.f2);
                jlb_previousPage.setForeground(Color.BLUE);
                jlb_previousPage.setText("<html><u>"+"上一页"+"</u></html>");
                jPanel.add(jlb_previousPage);
            }
            jlb_nextPage=new JLabel();
            jlb_nextPage.addMouseListener(this);
            jlb_nextPage.setFont(myFont.f2);
            jlb_nextPage.setForeground(Color.BLUE);
            jlb_nextPage.setText("<html><u>"+"下一页"+"</u></html>");
            jPanel.add(jlb_nextPage);
            this.add(jPanel);
        }
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==chenghu || jl==suishu || jl==chushou || jl== zhuangtai || jl==daiyou)
        {
            f=true;
        }else
        {
            for(int i=0;i<zhuangbei.length;i++)
            {
                if(jl==zhuangbei[i])
                {
                    f=true;
                    break;
                }
            }
        }
        return f;
    }
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==this.baishi)
        {
            this.game.setPanel(new BaishiTishi(this.game,this.npc));
        }else if(e.getSource()==this.panshi)
        {
            this.game.setPanel(new PanshiTishi(this.game,this.npc));
        }else if(e.getSource()==this.xueyi)
        {
            this.game.setPanel(new Xueyiwugong(this.game,this.npc,1));
        }else if(e.getSource()==this.shalu)
        {
            if(this.heroMap.isMassacre())
            {
                ZhangDou zd=this.game.hero.setjinruzdou(this.npc);

                this.game.setPanel(new ZhangdouJPanel(this.game,zd,true));
            }else
            {
                this.game.setPanel(new HepingTishi(this.game,this.npc));
            }

        }else if(e.getSource()==jlb_nextPage)
        {
            this.game.setPanel(new NPCXinxi(this.game,this.npc,(this.newPage+1)));
        }else if(e.getSource()==jlb_previousPage)
        {
            this.game.setPanel(new NPCXinxi(this.game,this.npc,(this.newPage-1)));
        }else if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            this.game.setPanel(new Renwu(this.game,1));
        }
    }

    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }

    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
